/**
 * 应用的核心js文件
 */
var grErp = angular.module('gr-erp', []);

grErp.controller('TestCtrl',['$scope',function($scope){
    $scope.checkVersion = function(){
        alert('hello there~');
    };
}]);
grErp.controller('StorageInCtrl',['$scope',function($scope){
    $scope.items = [{}];
    $scope.moreItem = function(){
        $scope.items.push({});

    }
    
    $scope.init=function(o){
//        alert(o);
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].inventory = $scope.product[proIndex].cnt;
        $scope.items[$index].id = $scope.product[proIndex].id;
    }

    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }

    $scope.save = function(){
        alert(JSON.stringify($scope.items));
    }
}]);
grErp.controller('EditProductCtr',['$scope',function($scope){

}]);
grErp.directive('isAlreadyTaken', function() {
    return {
        require: 'ngModel',
        link: function(scope, ele, attrs, ctrl) {
            ctrl.$parsers.push(function(val) {
                ctrl.$setValidity('emailAvailable', false);
                var emailTable = [
                    'K@gmail.com',
                    'a@gmail.com',
                    'v@gmail.com',
                    'l@gmail.com',
                    'e@gmail.com',
                    'z@gmail.com'];
                for (var i=0;i<emailTable.length;i+=1){
                    if(val==emailTable[i]){
                        ctrl.$setValidity('emailAvailable', true);
                        return;
                    }
                }
                return val;
            })
        }
    }
});
grErp.directive('ensureUnique', ['$http', function($http) {
    return {
        require: 'ngModel',
        link: function(scope, ele, attrs, c) {
            ele.bind('focus', function(evt) {
                scope.$apply(function() {
                    c.focused = true;
                });
            }).bind('blur', function(evt) {
                scope.$apply(function() {
                    c.focused = false;
                    var val = ele.val();
                    if(!val){
                        c.$setValidity('unique', true);
                        return;
                    }
                    $http({
                        method: 'POST',
                        url: '/gr_erp/index.php?s=/Api/Product/index',
                        params:{spu:val}
                    }).success(function(data, status, headers, cfg) {
                        if(data != "null"){
                            //存在
                            c.$setValidity('unique', false);
                        }
                    }).error(function(data, status, headers, cfg) {
                        console.log(data);
                    });
                });
            });
        }
    }
}]);
grErp.directive('select2', function (select2Query) {
    return {
        restrict: 'A',
        scope: {
            config: '=',
            ngModel: '=',
            select2Model: '='
        },
        link: function (scope, element, attrs) {
            // 初始化
            var tagName = element[0].tagName,
                config = {
                    allowClear: true,
                    multiple: !!attrs.multiple,
                    placeholder: attrs.placeholder || ' '   // 修复不出现删除按钮的情况
                };

            // 生成select
            if(tagName === 'SELECT') {
                var $element = $(element);
                delete config.multiple;
                $element
                    .prepend('<option value=""></option>')
                    .val('')
                    .select2(config);
                return false;
            }
        }
    }
});
grErp.factory('select2Query', function ($timeout) {
    return {
    }
});
grErp.controller('StorageOutCtrl',['$scope',function($scope){
    $scope.items = [{inventory:"",cnt:"",id:""}];
    $scope.moreItem = function(){
        $scope.items.push({inventory:"",cnt:"",id:""});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            $scope.items[$index].inventory = "";
            return false;
        }
        $scope.items[$index].inventory = $scope.product[proIndex].cnt;
        $scope.items[$index].id = $scope.product[proIndex].id;
    }
    $scope.cnt = function($index){
       var cnt = $scope.items[$index].cnt;
       var inventory = $scope.items[$index].inventory;
        console.log(cnt,inventory);
        if(isNaN(cnt)&&cnt!= undefined){
            $scope.items[$index].cnt = 1;
            alert("请输入数字");
        }else if(cnt<1){
            $scope.items[$index].cnt = 1;
            alert("最少出库数量为1");
        }else if(Number(cnt)>Number(inventory)){
            $scope.items[$index].cnt = inventory;
            alert("没有这么多库存");
        }

    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }
    $scope.save = function(){
        alert(JSON.stringify($scope.items));
    }
}]);
grErp.controller('RejectCtrl',['$scope',function($scope){
    $scope.items = [{inventory:"",cnt:"",id:""}];
    $scope.moreItem = function(){
        $scope.items.push({inventory:"",cnt:"",id:""});
    }
    $scope.init=function(o){
        $scope.product = o;
    }
    $scope.select = function($index,x){
        var proIndex = parseInt(x);
        if(isNaN(proIndex)){
            return false;
        }
        $scope.items[$index].id = $scope.product[proIndex].id;
    }
    $scope.removeItem = function($index){
        if(!confirm('是否确定删除?')) return;
        $scope.items.splice($index,1);
    }

}]);
